//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/
define(function (require, exports, module) {
    require('./Tools.js');
    /**
     * The name of the hand tool.
     * @const
     * @type {string}
     */
    WebPDF.Tools.TOOL_NAME_HAND = "Hand";

    /**
     * @extends {WebPDF.Tools.IReaderToolHandler}
     * @constructor
     * @ignore
     */
    WebPDF.Tools.HandToolHandler = function (readerApp) {
        console.log("HandtoolHandler")
        var _bMovePage = false,
             _oldPosY = 0,
             _oldPosX = 0,
             _isCursorInitialized = false,
             _readerApp = readerApp;
        var _oldScale = 0;
        var current = null;

        /**
         * initialize cursor
         * @private
         */
        function _initCursor() {
            var mainView = _readerApp.getMainView();
            if (mainView !== null) {
                if (!_isCursorInitialized) {
                    var docView = mainView.getDocView();
                    var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
                    if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11)
                        jqDocViewContainer.addClass("fwr-hand-cursor-ie");
                    else
                        jqDocViewContainer.addClass("fwr-hand-cursor");
                    _isCursorInitialized = true;
                }
            }
            else {
                //wait util the viewer has been created
                setTimeout(_initCursor, 100);
            }
        }
        // Initialization
        this.onInit = function () {
           
        };
        // Destroy
        this.onDestroy = function () {
        };
        //Get name of the tool
        this.getName = function () {
            return WebPDF.Tools.TOOL_NAME_HAND;
        };
        this.onActivate = function () {
            setTimeout(_initCursor, 100);
        };
        this.onDeactivate = function () {
            var mainView = _readerApp.getMainView();
            if (mainView) {
                var docView = mainView.getDocView();
                var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
                if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11)
                    jqDocViewContainer.removeClass("fwr-hand-cursor-ie");
                else
                    jqDocViewContainer.removeClass("fwr-hand-cursor");
            }
            _isCursorInitialized = false;
        };

        this.isEnabled = function () {
            return true;
        };
        this.isProcessing = function () {
            return _bMovePage;
        };

        // Mouse events
        this.onLButtonDown = function (event) {
            var docView = _readerApp.getMainView().getDocView();
            var docViewRect = docView.getDocViewClientRect();
            if (!WebPDF.RectUtils.ptInRect(docViewRect, event.pageX, event.pageY))
                return true;
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (!WebPDF.Common.containsNode(jqDocViewContainer.get(0), event.target, true)) {
                return false;
            }
            if (WebPDF.Environment.ieAtLeast11)
            {
                //让鼠标在拖动时不会变成文本选中状态,同时解决ie11下面拖动页面不正常的bug
                WebPDF.Common.preventDefaults(event, true);
            }
            _bMovePage = true;
            _oldPosY = event.pageY; //当前鼠标位置
            _oldPosX = event.pageX; //当前鼠标位置
            if (!WebPDF.Environment.mobile)
            {
            	if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11) {
            	    jqDocViewContainer.addClass("fwr-catch-cursor-ie").removeClass("fwr-hand-cursor-ie");
	            }
	            else {
	                jqDocViewContainer.addClass("fwr-catch-cursor").removeClass("fwr-hand-cursor");
	            }
            }
            return true;
        };
        this.onLButtonUp = function (event) {
            if (_bMovePage) {
                _bMovePage = false;
                var docView = _readerApp.getMainView().getDocView();
                var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
                if (!WebPDF.Environment.mobile)
                {
	                if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11) {
	                    jqDocViewContainer.removeClass("fwr-catch-cursor-ie").addClass("fwr-hand-cursor-ie");
	                }
	                else {
	                    jqDocViewContainer.removeClass("fwr-catch-cursor").addClass("fwr-hand-cursor");
	                }
                }

            }
            return true;
        };
        this.onLButtonDblClk = function (event) {
            return false;
        };
        this.onMouseMove = function (event) {
            if (!_bMovePage)
                return true;
            WebPDF.Common.preventDefaults(event, true);
            _readerApp.getMainView().getDocView().movePage(_oldPosX - event.pageX, _oldPosY - event.pageY);
            _oldPosY = event.pageY;
            _oldPosX = event.pageX;
            return true;
        };
        this.onRButtonDown = function (event) {
            return false;
        };
        this.onRButtonUp = function (event) {
            return false;
        };
        this.onRButtonDblClk = function (event) {
            return false;
        };
        this.onMouseWheel = function (event) {
            return false;
        };
        this.onMouseOver = function (event) {
            return false;
        };
        this.onMouseOut = function (event) {
            return false;
        };
        this.onMouseEnter = function (event) {
            return false;
        };
        this.onMouseLeave = function (event) {
            if (_bMovePage) {
                _bMovePage = false;
            }
            return true;
        };

        /**
         * 根据元素id获取页面索引 （注: 这种方式及其不好，后续需要修改）
         * @param elem
         * @returns {Number}
         * @private
         */
        function _getPageIndexByElem(elem) {
            var id = $(elem).attr("id");
            return  Number(id.substr(id.lastIndexOf("_") + 1));
        }

        this.onDoubleTap = function(event) {
            var docView = _readerApp.getMainView().getDocView();
            var curZoom = docView.getScale();
            var fitWidthScale = docView.getFitWidthScale(true);
            if (curZoom > fitWidthScale) {
                curZoom = fitWidthScale;
                docView.setFitWidth(true);
                docView.onZoomByPos(fitWidthScale, event.gesture.center.pageX, event.gesture.center.pageY,
                    _getPageIndexByElem(event.srcElement));
            }
            else {
                curZoom = fitWidthScale * 2;
                docView.setFitWidth(false);
                docView.onZoomByPos(curZoom, event.gesture.center.pageX, event.gesture.center.pageY,
                    _getPageIndexByElem(event.srcElement));
            }
            return true;
        };
        this.onPinchIn = function(event) {
            var pinchScale = event.gesture.scale;
            var docView = _readerApp.getMainView().getDocView();
            if (current != Hammer.detection.current) {
                _oldScale = 0;
            }
            current = Hammer.detection.current;
            var fitWidthScale = docView.getFitWidthScale(true);
            var curZoom = docView.getScale();
            if (_oldScale === 0) {
                _oldScale = curZoom;
            }
            else {
                curZoom = _oldScale;
            }
            var minScale = fitWidthScale;
            if (curZoom <= minScale) {
                return;
            }
            var tmpZoom = curZoom;
            var newScale = tmpZoom * pinchScale;
            if (Math.abs(newScale - tmpZoom) > 0.01) {
                var endX = event.gesture.center.pageX;
                var endY = event.gesture.center.pageY;
                var startX = event.gesture.startEvent.center.pageX;
                var startY = event.gesture.startEvent.center.pageY;
                var centerX = (endX + startX) / 2;
                var centerY = (endY + startY) / 2;
                if (newScale <= fitWidthScale) {
                    docView.setFitWidth(true);
                    docView.onZoomByPos(fitWidthScale, centerX, centerY, _getPageIndexByElem(event.srcElement));
                }
                else {
                    docView.onZoomByPos(newScale, centerX, centerY, _getPageIndexByElem(event.srcElement));
                }
            }
        };
        this.onPinchOut = function(event) {
            var pinchScale = event.gesture.scale;
            var docView = _readerApp.getMainView().getDocView();
            if (current != Hammer.detection.current) {
                _oldScale = 0;
            }
            current = Hammer.detection.current;

            var fitWidthScale = docView.getFitWidthScale(true);
            var curZoom = docView.getScale();
            if (_oldScale === 0) {
                _oldScale = curZoom;
            }
            else {
                curZoom = _oldScale;
            }
            var maxScale = fitWidthScale > WebPDF.Config.defaults.maxZoomSize ? fitWidthScale : WebPDF.Config.defaults.maxZoomSize;
            if (curZoom >= maxScale) {
                return;
            }
            var tmpZoom = curZoom;
            var newScale = tmpZoom * pinchScale;
            if (newScale - curZoom > 0.01) {
                if (newScale > fitWidthScale) {
                    docView.setFitWidth(false);
                }
                var endX = event.gesture.center.pageX;
                var endY = event.gesture.center.pageY;
                var startX = event.gesture.startEvent.center.pageX;
                var startY = event.gesture.startEvent.center.pageY;
                var centerX = (endX + startX) / 2;
                var centerY = (endY + startY) / 2;
                docView.onZoomByPos(newScale, centerX, centerY, _getPageIndexByElem(event.srcElement));
            }
            return true;
        };

        this.onHold = function(event) {
            var textSelectionTool = _readerApp.getToolHandlerByName(WebPDF.Tools.TOOL_NAME_SELECTTEXT);
            if(textSelectionTool){
                return textSelectionTool.onHold(event);
            }
            return false;
        };
    };

    return WebPDF.Tools.HandToolHandler;
});